home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / amusemen / vgatarot.3 / vgatarot / VGATarot-0.3.fix / vga3card.c < prev    next >
C/C++ Source or Header  |  1994-05-25  |  7KB  |  242 lines

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<strings.h>
  4. #include<unistd.h>
  5. #include<signal.h>
  6. #include<vga.h>
  7. #include<vgagl.h>
  8. #include<sys/time.h>
  9. #include<sys/types.h>
  10. #include<fcntl.h>
  11. #include"zsoftpcx.h"
  12.  
  13. typedef struct timeval t_tv;
  14. typedef struct timezone t_tz;
  15.     
  16. unsigned long giverand(unsigned long range,int seedadd);
  17.  
  18. void viewpcx(int XBeg,int YBeg,char *libname,char *indexname,int cnum,int orientation);
  19.  
  20. void north_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel);
  21.  
  22. void east_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel);
  23.  
  24. void south_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel);
  25.  
  26. void west_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel);        
  27.  
  28. void main(int argc,char *argv[]) {
  29.     char question[5000];
  30.     char name[200];
  31.     int hand[3][2] = {{0,0},{0,0},{0,0}};
  32.     int done=0;
  33.     int deckx[78] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  34.              0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  35.              0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };    
  36.     int vgamode=G640x480x256;
  37.     char posit[3][80] = {"Past","Present","Future"};
  38.  
  39.     int pos[3][2]={{85,160},{270,160},{455,160}};
  40.  
  41.     int seed,x,y;
  42.  
  43.     printf("\nVGATarot - Three Card V0.3\n");
  44.     printf("\nPlease enter your name (first and last) > ");
  45.     gets(name);
  46.     printf("Please enter your question\n> ");
  47.     gets(question);
  48.     
  49.     for(x=0,seed=0;x!=strlen(name);x++,seed+=name[x]);
  50.     for(x=0;x!=strlen(question);x++,seed+=question[x]);
  51.  
  52.     y=0;
  53.     done=0;
  54.     
  55.     while(!done) {
  56.         x=(int)giverand(78,seed);
  57.         if(deckx[x]==0) {
  58.             deckx[x]=1;
  59.             hand[y][0]=x; 
  60.             if((int)giverand(100,seed)>50) hand[y][1]=1; else hand[y][1]=0;
  61.             y++; } 
  62.         if(y==3) done=1; }
  63.  
  64.     if(!vga_hasmode(vgamode)) {
  65.         printf("mode is unsupported\n"); 
  66.         exit(-1); }
  67.         
  68.     vga_setmode(vgamode);
  69.     gl_setcontextvga(vgamode);
  70.     gl_enableclipping();
  71.     gl_setwritemode(WRITEMODE_OVERWRITE);
  72.     
  73.     printf("\n");
  74.  
  75.     for(x=0;x!=3;x++) {
  76.         if(hand[x][1]==0) seed=1; else seed=3;
  77.         printf("%-20s : %s %s\n",posit[x],deck[hand[x][0]],hand[x][1]==0?"":"Reversed");
  78.         viewpcx(pos[x][0],pos[x][1],"tarotlib.lbr","tarotlib.index",hand[x][0],seed); }
  79.         
  80.     printf("\n");
  81.  
  82.     getchar();
  83.     vga_setmode(TEXT);
  84.     
  85.     exit(1); }
  86.     
  87. unsigned long giverand(unsigned long range,int seedadd)
  88.     { 
  89.     t_tv tv;
  90.     t_tz tz;
  91.         
  92.     gettimeofday(&tv,&tz);
  93.     srandom((unsigned int)tv.tv_usec+(unsigned int)seedadd);
  94.     return (unsigned long)((random()%range)+1); }
  95.  
  96. void viewpcx(int XBeg,int YBeg,char *libname,char *indexname,int cnum,int orientation) { 
  97.     int fi; /* index file */
  98.     int fp; /* data file */
  99.     unsigned char *data_pix;
  100.     unsigned long foffset;
  101.     unsigned long flength;
  102.     int XPos,YPos,XMax,YMax;
  103.     zhead pcx;
  104.     Palette pal;
  105.     unsigned long x;
  106.     unsigned int y,z,inbyte;
  107.     unsigned char inc;
  108.  
  109.     if((fi=open(indexname,O_RDONLY))==-1) {
  110.         vga_setmode(TEXT);
  111.         printf("cannot open index file\n");
  112.         exit(-1); }
  113.         
  114.     lseek(fi,cnum*8,SEEK_SET); /* get the correct index */
  115.     
  116.     read(fi,&foffset,sizeof(foffset)); /* get the offset */
  117.     read(fi,&flength,sizeof(flength)); /* get the length */
  118.     
  119.     close(fi); /* close index file */
  120.     
  121.     if((fp=open(libname,O_RDONLY))==-1) { /* open library file */
  122.         vga_setmode(TEXT);
  123.         printf("cannot open library file\n");
  124.         exit(-1); }
  125.     
  126.     lseek(fp,foffset,SEEK_SET); /* go to correct offset in library */
  127.             
  128.     if(read(fp,&pcx,sizeof(pcx))!=sizeof(pcx)) { /* read PCX header information */
  129.         vga_setmode(TEXT);
  130.         printf("cannot read header information of picture %d\n",cnum);
  131.         exit(-1); }
  132.         
  133.     XMax=(int)pcx.x2; YMax=(int)pcx.y2; /* set the x and y dimensions of the picture */
  134.     
  135.     lseek(fp,128-sizeof(pcx),SEEK_CUR);
  136.     
  137.     if((data_pix=(char *)malloc(flength-128))==NULL) { /* get picture memory */
  138.         vga_setmode(TEXT);
  139.         printf("couldn't allocate picture data memory\n");
  140.         exit(-1); }    
  141.         
  142.     if(((unsigned long)read(fp,data_pix,(flength-128)))!=(flength-128)) { /* read picture data */
  143.         vga_setmode(TEXT);
  144.         printf("couldn't read in picture data\n");
  145.         exit(-1); }
  146.         
  147.     close(fp); /* close library file */
  148.     
  149.     data_pix+=(flength-128-768); /* get to the palette data */
  150.                 
  151.     for(x=0;x<255;x++) { /* set the palette data */
  152.         inc=*data_pix++;
  153.         pal.color[x].red=(inc/4);
  154.         inc=*data_pix++;
  155.         pal.color[x].green=(inc/4);
  156.         inc=*data_pix++;
  157.         pal.color[x].blue=(inc/4); }
  158.         
  159.     data_pix-=(flength-131); /* why 131?  I've no damn clue, but it works better than 128 */
  160.         
  161.     gl_setpalette(&pal); /* set the palette */
  162.     
  163.     switch(orientation) { /* set up correct orientation parameters */
  164.         case 1 : /* north */ 
  165.                     XMax=(int)pcx.x2+1;
  166.                     YMax=(int)pcx.y2;
  167.                     XPos=XBeg; 
  168.                     YPos=YBeg; 
  169.                     break;
  170.         case 2 : /* east */ 
  171.                     XMax=(int)pcx.x2+1;
  172.                     YMax=(int)pcx.y2;
  173.                     XPos=YBeg+YMax;
  174.                     YPos=XBeg;
  175.                     break;
  176.         case 3 : /* south */ 
  177.                     XMax=(int)pcx.x2+1;
  178.                     YMax=(int)pcx.y2;
  179.                     XPos=XBeg+XMax; 
  180.                     YPos=YBeg+YMax; 
  181.                     break;
  182.         case 4 : /* west */ 
  183.                     XMax=(int)pcx.x2+1;
  184.                     YMax=(int)pcx.y2;
  185.                     XPos=YBeg;
  186.                     YPos=XBeg+XMax;
  187.                     break; }
  188.     
  189.     for(x=0;x<(XMax*YMax);) { /* decode the picture */
  190.         inbyte=(unsigned int)*data_pix++;
  191.         if(inbyte>0xc0) { /* RLE compression */
  192.             inbyte-=0xc0;
  193.             y=(unsigned int)*data_pix++;
  194.             for(z=0;z<inbyte;z++) {
  195.                 x++;
  196.                 switch(orientation) {
  197.                     case 1 : north_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,y); break;
  198.                     case 2 : east_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,y); break;                    
  199.                     case 3 : south_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,y); break;
  200.                     case 4 : west_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,y); break; } } }
  201.         else {
  202.             x++;
  203.             switch(orientation) {
  204.                 case 1 : north_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,inbyte); break;
  205.                 case 2 : east_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,inbyte); break;                                    
  206.                 case 3 : south_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,inbyte); break;
  207.                 case 4 : west_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,inbyte); break; } } }
  208.                 
  209.         free(data_pix); } /* free the data, we are done */
  210.  
  211. void north_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel) {
  212.     if(*y>(ym+yb)) return;
  213.     gl_setpixel(*x,*y,pixel);
  214.     *x=*x+1;
  215.     if(*x==(xm+xb)) {
  216.         *x=xb;
  217.         *y=*y+1; } }
  218.         
  219. void south_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel) { 
  220.     if(*y==yb) return;
  221.     gl_setpixel(*x,*y,pixel);
  222.     *x=*x-1;
  223.     if(*x==xb) {
  224.         *x=xm+xb;
  225.         *y=*y-1; } }        
  226.         
  227. void west_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel) {
  228.     if(*x>(ym+yb)) return;
  229.     gl_setpixel(*x,*y,pixel);
  230.     *y=*y-1;
  231.     if(*y==xb) {
  232.         *y=xb+xm;
  233.         *x=*x+1; } }
  234.         
  235. void east_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel) {
  236.     if((*x)<yb) return;
  237.     gl_setpixel(*x,*y,pixel);
  238.     *y=*y+1;
  239.     if(*y==(xm+xb)) {
  240.         *y=xb;
  241.         *x=*x-1; } }
  242.